package com.lj.auth.config;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.log.SaLog;
import cn.dev33.satoken.log.SaLogForConsole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author luojing
 * @since 2025/4/22 17:21
 * sa-token 日志实现，默认是System.out.println打印到控制台的，这里通过Slf4j打印
 */
@Slf4j
@Component
public class SaLogForSlf4j implements SaLog {
    @Override
    public void trace(String str, Object... args) {
        if (canPrint(SaLogForConsole.trace)) {
            log.trace(SaLogForConsole.TRACE_PREFIX + str, args);
        }
    }

    @Override
    public void debug(String str, Object... args) {
        if (canPrint(SaLogForConsole.debug)) {
            log.debug(SaLogForConsole.DEBUG_PREFIX + str, args);
        }
    }

    @Override
    public void info(String str, Object... args) {
        if (canPrint(SaLogForConsole.info)) {
            log.info(SaLogForConsole.INFO_PREFIX + str, args);
        }
    }

    @Override
    public void warn(String str, Object... args) {
        if (canPrint(SaLogForConsole.warn)) {
            log.warn(SaLogForConsole.WARN_PREFIX + str, args);
        }
    }

    @Override
    public void error(String str, Object... args) {
        if (canPrint(SaLogForConsole.error)) {
            log.error(SaLogForConsole.ERROR_PREFIX + str, args);
        }
    }

    @Override
    public void fatal(String str, Object... args) {
        if (canPrint(SaLogForConsole.fatal)) {
            log.error(SaLogForConsole.FATAL_PREFIX + str, args);
        }
    }

    private boolean canPrint(int level) {
        SaTokenConfig config = SaManager.getConfig();
        return config.getIsLog() && level >= config.getLogLevelInt();
    }
}
